package com.whut.work.comment.controller;

import com.whut.work.base.model.Page;
import com.whut.work.comment.model.Comment;
import com.whut.work.comment.service.ICommentService;
import com.whut.work.score.service.IScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zouyao on 2017/3/15.
 */
@Controller
@RequestMapping("/comment")
public class CommentCtrl {

    @Autowired
    private ICommentService commentService;
    @Autowired
    private IScoreService scoreService;

    @RequestMapping(value="/getCommentRecordByUserId",method= RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> getCommentRecordByUserId(HttpServletRequest request,Integer studentId,Integer teacherId){
        Map<String,Object> returnMap = new HashMap<String,Object>();

        try {
            Map<String,Object> result = commentService.getCommentRecordByUserId(studentId,teacherId);

            returnMap.put("value", result.get("value"));
            returnMap.put("message", result.get("message"));
            returnMap.put("success", true);
        } catch (Exception e) {
            returnMap.put("message", "异常：获取失败!");
            returnMap.put("success", false);
            e.printStackTrace();
        }
        return returnMap;
    }

    @RequestMapping(value="/setCommentForTeacher",method= RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> setCommentForTeacher(HttpServletRequest request,String comment, Integer studentId, Integer teacherId){
        Map<String,Object> returnMap = new HashMap<String,Object>();

        try {
            Map<String,Object> result = commentService.setCommentForTeacher(comment,studentId,teacherId);

            returnMap.put("value", result.get("value"));
            returnMap.put("message", result.get("message"));
            returnMap.put("success", true);
        } catch (Exception e) {
            returnMap.put("message", "异常：操作失败!");
            returnMap.put("success", false);
            e.printStackTrace();
        }
        return returnMap;
    }

    //教师自我评价
    @RequestMapping(value="/setCommentScoreForTeacher",method= RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> setCommentScoreForTeacher(HttpServletRequest request,String comment,Integer score, Integer studentId, Integer teacherId){
        Map<String,Object> returnMap = new HashMap<String,Object>();

        try {
            Map<String,Object> resultComment = commentService.setCommentForTeacher(comment,studentId,teacherId);
            Map<String,Object> resultScore = scoreService.setScoreForTeacher(score,studentId,teacherId);

            returnMap.put("value", resultComment.get("value"));
            returnMap.put("message", resultComment.get("message"));
            returnMap.put("success", true);
        } catch (Exception e) {
            returnMap.put("message", "异常：操作失败!");
            returnMap.put("success", false);
            e.printStackTrace();
        }
        return returnMap;
    }

    @RequestMapping(value="/getCommentListOfOneTeacher",method= RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> getCommentListOfOneTeacher(HttpServletRequest request,Integer currentPage, Integer pageSize,Integer id){
        Map<String,Object> returnMap = new HashMap<String,Object>();

        try {
            Page<Comment> resultComment = commentService.getCommentListOfOneTeacher(currentPage,pageSize,id);

            returnMap.put("value", resultComment);
            returnMap.put("message", "获取成功");
            returnMap.put("success", true);
        } catch (Exception e) {
            returnMap.put("message", "异常：操作失败!");
            returnMap.put("success", false);
            e.printStackTrace();
        }
        return returnMap;
    }

}
